# -*-coding:utf-8-*-
'''
File Create by Author:Backpackerxl
Create File Time:2021/10/13/013 21:00
Program IDE:PyCharm
File Name:UserController.py
  
'''
from control.MangerController import MangerController as u_control
from service.UserService import UserService as user_service
from util.WatchInput import WatchInput as u_input


class UserController:

    def _lock_user(self, blog):
        res = u_control.login(self, u_input.WatchInput(self, "请输入管理员账号"), u_input.WatchInputPwd(self, "请输入密码"))
        info = "请输入锁定卡号" if blog == 1 else "请输入解锁卡号"
        if res[0]:
            u_close_id = u_input.WatchInput(self, info)
            u_res = user_service.find_by_user_bank_id(self, u_close_id)
            user = u_res[0]
            if user != None:
                u_res[1].remove(user.user_to_dic())
                user.set_user_use_state(blog)
                u_res[1].append(user.user_to_dic())
                info = "锁定成功" if blog == 1 else "解锁成功"
                print(info)
        else:
            print(res[1])

    def _change_sex(self, blog):
        return "先生" if blog == 1 else "女士"

    def insert_user(self):
        res = user_service.insert_user(self)
        user = res[0]
        if user != None:
            res[1].append(user.user_to_dic())
            print("恭喜您，注册成功...")
        else:
            print(":) 对不起注册失败...")
            for info in res[2]:
                print(":)", info)

    def look_money(self):
        res = user_service.look_money(self)
        if len(res) != 0:
            user = res[1]
            if res[0]:
                print(
                    f'尊敬的{user.get_user_name()}{self._change_sex(self, user.get_user_sex())},您的账户余额为{user.get_user_money()}元')
            else:
                print(
                    f":) 对不起{user.get_user_name()}{self._change_sex(self, user.get_user_sex())},您的账户已被冻结。暂时无法使用查询功能，请先解锁。")

    def take_off_money(self):
        res = user_service.get_user(self)
        if len(res[0]) != 0:
            user = res[0][1]
            if res[0][0]:
                get_money = int(u_input.WatchInput(self, "请输入取款金额"))
                if get_money < 0:
                    print(":) 取款失败，取款金额不能为负数...")
                elif get_money > user.get_user_money():
                    print(f":) 取款失败，余额不足，最多可取{user.get_user_money()}元")
                elif user.get_user_money() == 0:
                    print(f":) 取款失败，余额为0元")
                else:
                    res[1].remove(user.user_to_dic())
                    user.set_user_money(user.get_user_money() - get_money)
                    res[1].append(user.user_to_dic())
                    print(f"取款成功，余额为{user.get_user_money()}元")
            else:
                print(
                    f":) 对不起{user.get_user_name()}{self._change_sex(self, user.get_user_sex())},您的账户已被冻结。暂时无法使用取款功能，请先解锁。")

    def move_to_money(self):
        res = user_service.get_user(self)
        if len(res[0]) != 0:
            user = res[0][1]
            if res[0][0]:
                move_to_id = u_input.WatchInput(self, "请输入要转账的卡号")
                res1 = user_service.find_by_user_bank_id(self, move_to_id)
                move_to_money = int(u_input.WatchInput(self, "请输入要转账的金额"))
                is_move_money = u_input.WatchInput(self, "确认要转账吗(y/n)")
                if is_move_money == "y":
                    is_password = u_input.WatchInputPwd(self, "请确认密码")
                    if is_password == user.get_user_password():
                        if move_to_money <= user.get_user_money():
                            user1 = res1[0]
                            if user1 != None:
                                res[1].remove(user.user_to_dic())
                                res[1].remove(user1.user_to_dic())
                                user.set_user_money(user.get_user_money() - move_to_money)
                                user1.set_user_money(user1.get_user_money() + move_to_money)
                                res[1].append(user.user_to_dic())
                                res[1].append(user1.user_to_dic())
                                print(f"转账成功，余额为{user.get_user_money()}元")
                            else:
                                print(":) 转账失败，转账用户不存在")
                        else:
                            print(":) 转账失败，余额不足")
                    else:
                        print(":) 转账失败，密码错误")
                else:
                    print("已取消")
            else:
                print(
                    f":) 对不起{user.get_user_name()}{self._change_sex(self, user.get_user_sex())},您的账户已被冻结。暂时无法使用存款功能，请先解锁。")

    def take_in_money(self):
        res = user_service.get_user(self)
        if len(res[0]) != 0:
            user = res[0][1]
            if res[0][0]:
                set_money = int(u_input.WatchInput(self, "请输入存款金额"))
                if set_money < 0:
                    print(":) 存款失败，存款金额不能为负数...")
                else:
                    res[1].remove(user.user_to_dic())
                    user.set_user_money(user.get_user_money() + set_money)
                    res[1].append(user.user_to_dic())
                    print(f"存款成功，余额为{user.get_user_money()}元")
            else:
                print(
                    f":) 对不起{user.get_user_name()}{self._change_sex(self, user.get_user_sex())},您的账户已被冻结。暂时无法使用转账功能，请先解锁。")

    def close_lock_user(self):
        self._lock_user(self, 1)

    def open_lock_user(self):
        self._lock_user(self, 0)

    def preservation_all_data(self):
        user_service.preservation_all_data(self)
        print("正在保存...")
